package com.lionel.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lionel.bean.*;
import com.lionel.cache.FileManagement;
import com.lionel.constant.Constants;
import org.apache.commons.lang3.StringUtils;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author: liucan
 * @Date: 2019/2/2 16:49
 * @desc: 操作存储介质txt
 **/
public class FileUtil {
    /**
     * @param baseDomain 需要添加的数据
     * @Method: addData
     * @Return: boolean 如果为true添加成功 如果为false 添加失败
     * @Author: liucan
     * @Desc: 保存数据
     */
    public static boolean addData(Object baseDomain) {
        boolean result = false;
        FileWriter fileWriter = getFileWriter(baseDomain);
        if (fileWriter != null) {
            try {
                fileWriter.write(JSON.toJSONString(baseDomain) + Constants.SEPARATOR);
                result = true;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * @Method: getFileWriter
     * @param baseDomain
     * @Return: java.io.FileWriter
     * @Author: liucan
     * @Desc: 获取对应文件的输出流
     */
    public static FileWriter getFileWriter(Object baseDomain) {
        FileManagement fileManagement = new FileManagement();
        FileWriter fileWriter = null;
        if (null != baseDomain) {
            if (baseDomain instanceof User) {
                fileWriter = fileManagement.getFileWriter(User.class);
            } else if (baseDomain instanceof Book) {
                fileWriter = fileManagement.getFileWriter(Book.class);
            } else if (baseDomain instanceof Library) {
                fileWriter = fileManagement.getFileWriter(Library.class);
            } else if (baseDomain instanceof Record) {
                fileWriter = fileManagement.getFileWriter(Record.class);
            } else if (baseDomain instanceof Role) {
                fileWriter = fileManagement.getFileWriter(Role.class);
            }
        }
        return fileWriter;
    }

    /**
     * @param baseDomain
     * @Method: getFileReader
     * @Return: java.io.FileReader
     * @Author: liucan
     * @Desc: 获取指定文件的输入流
     */
    public static FileReader getFileReader(Class baseDomain) {
        FileManagement fileManagement = new FileManagement();
        FileReader fileReader = null;
        if (null != baseDomain) {
            fileReader = fileManagement.getFileReader(baseDomain);
        }
        return fileReader;
    }

    /**
     * @Method: getAllData
     * @param baseDomain
     * @Return: java.lang.String[]
     * @Author: liucan
     * @Desc: 获取指定文件的所有数据
     */
    public static  String[] getAllData(Class baseDomain){
        String[] result=null;
        if (null != baseDomain) {
            FileReader reader = getFileReader(baseDomain);
            StringBuffer allData=new StringBuffer();
            char [] charArr=new char[1024];
            try {
                int len=0;
                while ((len = reader.read(charArr)) != -1) {
                    allData.append(new String(charArr,0,len-1));
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (StringUtils.isNotBlank(allData)){
                result = StringUtils.split(allData.toString(), Constants.SEPARATOR);
            }
        }
        return result;
    }

    /**
     * @param baseDomain
     * @Method: getDataById
     * @Return: java.lang.Object
     * @Author: liucan
     * @Desc: 根据id获取指定对象
     */
    public static BaseDomain getDataById(BaseDomain baseDomain) {
        BaseDomain result = null;
        Long id = baseDomain.getId();
        Class cls= getClassByObj(baseDomain);
        if (null != baseDomain && null != id) {
            String[] allData = getAllData(getClassByObj(baseDomain));
            for (int i = 0; i < allData.length; i++) {
                BaseDomain object = (BaseDomain) JSONObject.parse(allData[i]);
                if (id.equals(object.getId())&& object.getYn()) {
                    result = object;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * @Method: getClassByObj
     * @param baseDomain
     * @Return: java.lang.Class
     * @Author: liucan
     * @Desc: 根据对象获取对象的class
     */
    public static Class getClassByObj(BaseDomain baseDomain) {
        if (baseDomain instanceof User) {
            return User.class;
        } else if (baseDomain instanceof Book) {
            return Book.class;
        } else if (baseDomain instanceof Library) {
            return Library.class;
        } else if (baseDomain instanceof Record) {
            return Record.class;
        } else if (baseDomain instanceof Role) {
            return Role.class;
        }
        return null;
    }

}
